package com.shujia.tf;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.functions.RichMapFunction;
import org.apache.flink.api.java.tuple.Tuple3;
import org.apache.flink.configuration.Configuration;

import java.text.SimpleDateFormat;
import java.util.Date;

public class RealTImeCardDayFlowMapper extends RichMapFunction<String, Tuple3<Long, String, Long>> {

    private SimpleDateFormat format;

    @Override
    public void open(Configuration parameters) throws Exception {
        format = new SimpleDateFormat("yyyyMMdd");

    }

    @Override
    public Tuple3<Long, String, Long> map(String car) throws Exception {
        //解析数据获取卡口和时间

        //将一个json字符串转换成json对象，可以通过json对象通过key获取value
        JSONObject carJson = JSON.parseObject(car);

        Long card = carJson.getLong("card");

        Long time = carJson.getLong("time");

        Date date = new Date(time * 1000);

        //将时间戳转换成当天
        String day = format.format(date);

        //返回数据
        return Tuple3.of(card, day, 1L);

    }
}
